package com.slg.android.manyanime;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.slg.android.manyanime.adapter.NoticeListAdapter;
import com.slg.android.manyanime.bean.NoticeList;
import com.slg.android.manyanime.bean.User;
import com.slg.android.manyanime.utils.DataStore;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;


public class MyNoticeListActivity extends Activity implements
        View.OnClickListener,PullToRefreshBase.OnRefreshListener2<ListView> {

    private List<NoticeList> noticeLists;
    private NoticeListAdapter noticeListAdapter;
    private PullToRefreshListView refreshListView;
    private AlertDialog dialog;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_notice_list);

        //todo 上下拉刷新
        //获取PullToRefresh
        refreshListView = (PullToRefreshListView) findViewById(R.id.my_notice_pull_to_refresh);
        ListView listView = refreshListView.getRefreshableView();
        //设置上拉下拉都支持
        refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        refreshListView.setRefreshingLabel("正在加载中");
        refreshListView.setReleaseLabel("松开我就努力");
        //PullToRefreshListView的刷新事件
        refreshListView.setOnRefreshListener(this);

        View view = findViewById(R.id.my_notice_list_return_img);
        view.setOnClickListener(this);
        view = findViewById(R.id.my_notice_list_clear_txt);
        view.setOnClickListener(this);
        //我的通知列表
//        ListView listView = (ListView) findViewById(R.id.my_notice_list);

        noticeLists = new LinkedList<NoticeList>();

        noticeListAdapter = new NoticeListAdapter(this, noticeLists);

        user = DataStore.getDataStore().getUser();
        if(listView != null){
        listView.setAdapter(noticeListAdapter);

            HttpUtils httpUtils = new HttpUtils();
            httpUtils.send(
                   HttpRequest.HttpMethod.GET ,
                   "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=百度&m=New&a=getSysmsg&uid="+ user.getId()+"&limit=100",
                    new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                            String result = objectResponseInfo.result;
                            LinkedList<NoticeList> notices= new LinkedList<NoticeList>();
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                JSONArray jsonArray = jsonObject.getJSONArray("list");
                                for(int i = 0; i<jsonArray.length();i++){
                                    JSONObject noticeJSONObject = jsonArray.getJSONObject(i);
                                    NoticeList noticeList = new NoticeList();
                                    noticeList.parseJSON(noticeJSONObject);
                                    notices.add(noticeList);
                                }
                                noticeLists.addAll(notices);
                                noticeListAdapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        @Override
                        public void onFailure(HttpException e, String s) {
                            Toast.makeText(MyNoticeListActivity.this, "网络问题", Toast.LENGTH_LONG).show();
                        }
                    }
            );
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id){
            case R.id.my_notice_list_return_img:
                finish();
                break;
            case R.id.my_notice_list_clear_txt:
                //todo 通知删除
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                LayoutInflater inflater = LayoutInflater.from(this);
                View view = inflater.inflate(R.layout.item_my_notice_remote_all, null);
                Button sure = (Button) view.findViewById(R.id.dialog_sure);
                sure.setOnClickListener(this);
                Button negative  = (Button) view.findViewById(R.id.dialog_negative);
                negative.setOnClickListener(this);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
                break;
            case R.id.dialog_sure:
                if (noticeLists != null) {
                    noticeLists.clear();
                    noticeListAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.dialog_negative:
                dialog.dismiss();
                break;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        //下拉刷新
//         DownloadTask task = new DownloadTask();
//        task.execute();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        //上拉拉加载
        DownloadTask task = new DownloadTask();
        task.execute();

    }

    class DownloadTask extends AsyncTask<String,Integer,String> {
        @Override
        protected String doInBackground(String... params) {

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=百度&m=New&a=getSysmsg&uid="+user.getId()+"&limit=100";
        }
        protected void onPostExecute(String s){
            refreshListView.onRefreshComplete();
        }

    }
}
